package cn.topevery.garden.entity.serializer;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
//import com.alibaba.fastjson2.JSONReader;
//import com.alibaba.fastjson2.JSONWriter;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.dto.PointDto;
import org.locationtech.jts.geom.Geometry;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by admin on 2019/12/28.
 */
public class GeometrySerializer implements ObjectSerializer, ObjectDeserializer {

/*    @Override
    public long getFeatures() {
        return ObjectSerializer.super.getFeatures();
    }

    @Override
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        ObjectSerializer.super.write(jsonWriter, object, fieldName, fieldType, features);
    }*/

    // 序列化，将geometry类型转换为字符串，然后写入到json中
    @Override
    public void write(JSONSerializer jsonSerializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        Geometry geom = (Geometry) object;
        List<List<PointDto>> points = GeometryHelper.convertGeometryToPoint(geom);
        jsonSerializer.write(points);
    }

/*    @Override
    public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        return ObjectDeserializer.super.readObject(jsonReader, fieldType, fieldName, features);
    }*/

    //反序列化过程
    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        return null;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
